<?php

?>
<div class="hidden" id="addSen-cont">
    <form class="well form-inline margin-horisontal login" action="" method="POST" name="addSenForm" id="addSenForm">
        <fieldset>




            <div id="addSenFormFields" class="fixSize" style="max-height: 300px;">

            </div>

            <hr>

            <div class="control-group " id="cg-sen-allsen">
                <div class="controls noWrap">
                    <input type="checkbox" id="sen-allsen" name="sen-allsen" >
                    <label class="control-label noWrap" for="sen-allsen" >Все</label>
                </div>
            </div>


            <div class="form-actions">
                <button type="submit" class="btn btn-primary" id="addSen-submit"><i class="icon-ok icon-white"></i> Сохранить</button>
                <button type="reset" class="btn btn-danger" id="addSen-cancel"><i class="icon-remove icon-white"></i> Отменить</button>
            </div>
        </fieldset>
    </form>
</div>

<div class="hidden" id="addSen-sen-template">
    <div class="control-group " id="cg-sen-{%name%}">
        <div class="controls noWrap">
            <input type="checkbox" id="sen-{%name%}" name="sen-{%name%}" >
            <label class="control-label noWrap" for="sen-{%name%}" >{%name%}</label>
        </div>
    </div>
</div>


<script>

function addSenFormOpen(table, onOpen, callback) {
    callback = callback || function () {};
    onOpen = onOpen || function () {};

    jsonRequest(
        'views.php?tblSensors',
        {
            action  : '<?php echo ACTIONS::DATA_SENSORS_GET; ?>',
            table   : table
        },

        function(data) {
            if(checkSession(data)) {
                if (!data.state) {
                    uiAlert(data.message,'Error');
                    return;
                }

                if (data.hasOwnProperty('response_data') && data.response_data) {
                    var resp = data.response_data;

                    if (resp.hasOwnProperty('sensors')) {

                        addSenFormRefreshSensors(resp.sensors);

                        Elm.addSen.dialog('open');
                        if (typeof(callback) == 'function') Elm.addSenForm.successCallback = callback;
                        else Elm.addSenForm.successCallback = function () {};

                        onOpen();
                    }
                }
            }
            else {
                Elm.loginFormModal.dialog('open');
                Elm.loginFormModal.successCallback = function(){
                    addSenFormOpen(table, onOpen, callback);
                };
            }
        },

        function() {
            uiAlert('Ошибка соединения','Error');
        }
    );

    //
}


function addSenFormRefresh () {
    var jsen = Elm.addSenFormFields.find('input[type="checkbox"]:checked');
    if (jsen.length) $('#sen-allsen').removeAttr('checked');
    else $('#sen-allsen').attr('checked','checked');
}


function addSenFormRefreshSensors(sensors) {
    Elm.addSenFormFields.empty();
    var html = $('#addSen-sen-template').html();

    for (var i in sensors) {
        if (!sensors.hasOwnProperty(i)) continue;

        var senhtml = html.gReplace('{%name%}', sensors[i]);
        Elm.addSenFormFields.append(senhtml);
    }

    var jsen = Elm.addSenFormFields.find('input[type="checkbox"]');
    jsen.change(function() {
        addSenFormRefresh ();
    });
}

function addSenFormMarkSensors(sensors) {
    //console.log(sensors);
    for (var i in sensors) {
        if (sensors.hasOwnProperty(i)) {
            $('#sen-'+sensors[i]).attr('checked','checked');
        }
    }
    addSenFormRefresh ();

}


function isAllChecked() {
    return $('#sen-allsen:checked').length;
}



    $(function() {
        Elm.addSen = $('#addSen-cont');
        Elm.addSenForm = $('#addSenForm');
        Elm.addSenFormFields = $('#addSenFormFields');
        Elm.addSenForm.successCallback = function(data) {};

        $('#addSen-cancel').click(function(){
            Elm.addSen.dialog('close');
            return false;
        });

        Elm.addSen.dialog({
            width       : 350,
            modal       : true,
            resizable   : false,
            title       : "Датчики",
            autoOpen    : false,
            buttons     : {}
        }).removeClass('hidden');



        $('#sen-allsen').change(function() {
            var jsenCh = Elm.addSenFormFields.find('input[type="checkbox"]:checked');
            if (isAllChecked()) {
                jsenCh.removeAttr('checked')/*.attr('disabled','disabled')*/;
            }
            return false;
            //else jsen.removeAttr('disabled');
        });





        Elm.addSenForm.submit(function(){
            if (
                    Elm.addSenForm.hasOwnProperty('successCallback')
                    && (typeof(Elm.addSenForm.successCallback) == 'function')
                ) {

                    var sensors = [];
                    if (!isAllChecked()) {
                        var jsen = Elm.addSenFormFields.find('input[type="checkbox"]:checked');//[checked]

                        jsen.each(function(){
                            var name = this.id.replace('sen-','');
                            sensors.push(name);
                        });
                    }



                    var cb = Elm.addSenForm.successCallback;
                    Elm.addSenForm.successCallback = function() {};
                    cb(sensors);
                }
            Elm.addSen.dialog('close');
            return false;
        });

    });




</script>